package com.example.orderflow.service.impl;

import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.example.orderflow.entity.enums.PageSize;
import com.example.orderflow.entity.query.OrderIdGenerationQuery;
import com.example.orderflow.entity.po.OrderIdGeneration;
import com.example.orderflow.entity.vo.PaginationResultVO;
import com.example.orderflow.entity.query.SimplePage;
import com.example.orderflow.mappers.OrderIdGenerationMapper;
import com.example.orderflow.service.OrderIdGenerationService;
import com.example.orderflow.utils.StringTools;


/**
 *  业务接口实现
 */
@Service("orderIdGenerationService")
public class OrderIdGenerationServiceImpl implements OrderIdGenerationService {

	@Resource
	private OrderIdGenerationMapper<OrderIdGeneration, OrderIdGenerationQuery> orderIdGenerationMapper;


	/**
	 * 根据条件查询列表
	 */
	@Override
	public List<OrderIdGeneration> findListByParam(OrderIdGenerationQuery param) {
		return this.orderIdGenerationMapper.selectList(param);
	}

	/**
	 * 根据条件查询列表
	 */
	@Override
	public Integer findCountByParam(OrderIdGenerationQuery param) {
		return this.orderIdGenerationMapper.selectCount(param);
	}

	/**
	 * 分页查询方法
	 */
	@Override
	public PaginationResultVO<OrderIdGeneration> findListByPage(OrderIdGenerationQuery param) {
		int count = this.findCountByParam(param);
		int pageSize = param.getPageSize() == null ? PageSize.SIZE15.getSize() : param.getPageSize();

		SimplePage page = new SimplePage(param.getPageNo(), count, pageSize);
		param.setSimplePage(page);
		List<OrderIdGeneration> list = this.findListByParam(param);
		PaginationResultVO<OrderIdGeneration> result = new PaginationResultVO(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), list);
		return result;
	}

	/**
	 * 新增
	 */
	@Override
	public Integer add(OrderIdGeneration bean) {
		return this.orderIdGenerationMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	@Override
	public Integer addBatch(List<OrderIdGeneration> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.orderIdGenerationMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	@Override
	public Integer addOrUpdateBatch(List<OrderIdGeneration> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.orderIdGenerationMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 多条件更新
	 */
	@Override
	public Integer updateByParam(OrderIdGeneration bean, OrderIdGenerationQuery param) {
		StringTools.checkParam(param);
		return this.orderIdGenerationMapper.updateByParam(bean, param);
	}

	/**
	 * 多条件删除
	 */
	@Override
	public Integer deleteByParam(OrderIdGenerationQuery param) {
		StringTools.checkParam(param);
		return this.orderIdGenerationMapper.deleteByParam(param);
	}

	/**
	 * 根据Id获取对象
	 */
	@Override
	public OrderIdGeneration getOrderIdGenerationById(Integer id) {
		return this.orderIdGenerationMapper.selectById(id);
	}

	/**
	 * 根据Id修改
	 */
	@Override
	public Integer updateOrderIdGenerationById(OrderIdGeneration bean, Integer id) {
		return this.orderIdGenerationMapper.updateById(bean, id);
	}

	/**
	 * 根据Id删除
	 */
	@Override
	public Integer deleteOrderIdGenerationById(Integer id) {
		return this.orderIdGenerationMapper.deleteById(id);
	}

	/**
	 * 获取最新数据
	 */
	@Override
	public OrderIdGeneration getLatestOrderIdGeneration() {
		return this.orderIdGenerationMapper.getLatestOrderIdGeneration();
	}

	/**
	 * 根据订单类型获取最新数据
	 */
	@Override
	public OrderIdGeneration getLatestOrderIdGenerationByOrderType(String orderType){
		return this.orderIdGenerationMapper.getLatestOrderIdGenerationByOrderType(orderType);
	};


	/**
	 * 生成最新订单号
	 */
	@Override
	public String generateNewOrderId(String orderTypeUse){

		Calendar calendar = Calendar.getInstance();
		int newYear = calendar.get(Calendar.YEAR);
		int newMonth = calendar.get(Calendar.MONTH)+1;

		OrderIdGeneration orderIdGeneration = this.getLatestOrderIdGenerationByOrderType(orderTypeUse);

		if(String.valueOf(newYear) .equals(orderIdGeneration.getYear()) && String.format("%02d",newMonth) .equals(orderIdGeneration.getMonth())){
			int end = Integer.parseInt(orderIdGeneration.getEnd())+1;
			String newEnd = String.format("%03d", end);
			return orderTypeUse+String.valueOf(newYear)+String.format("%02d", newMonth)+newEnd;
		}else {
			return orderTypeUse+String.valueOf(newYear)+String.format("%02d", newMonth)+"001";
		}
	};

	/**
	 * 根据订单编号存入
	 */
	@Override
	public Integer addOrderIdGeneration(String orderId){

		String orderType = orderId.substring(0, 2);
		String year = orderId.substring(2, 6);
		String month = orderId.substring(6, 8);
		String end = orderId.substring(8, 11);

		OrderIdGeneration orderIdGeneration = new OrderIdGeneration();
		orderIdGeneration.setOrderType(orderType);
		orderIdGeneration.setYear(year);
		orderIdGeneration.setMonth(month);
		orderIdGeneration.setEnd(end);

		this.add(orderIdGeneration);

		return 1;
	}
}